package com.ghyf.mplay.view;

//import com.ghyf.mplay.util.LogUtil;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.widget.TextView;

/**
 * 显示时间的控件
 * @author 陈小红
 *
 */
public class MTime extends TextView {
//	private static final String TAG="MTime";

	
	private int module_type; // 模块类型
	
	private int zOrder; // 模块的ZOrder
	
	private String file_version;	//文件版本
	
	private String module_name;	//控件名
	
	private int module_uid;//控件标识
	
	private int module_gid;//控件组标识
	
	private MTime mTime;
	private boolean show_second;
	public boolean runFlag = true;
//	private Thread myThread;
	private String timeStr; // 当前时间的字符串

	@SuppressLint("HandlerLeak")
	public Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			// 显示当前时间的字符串
			mTime.setText(timeStr);
		};
	};

	public MTime(Context context) {
		super(context);
	}

	// 显示当前时间的线程
//	private Runnable myRunnable = new Runnable() {
//		private long currentTimeMillis;
//		private Time time = new Time();
//		private String hourStr="";
//		private String minStr="";
//		private String secStr="";
//
//		@Override
//		public void run() {
//			while (runFlag) {
//				// 获取当前时间的时，分，秒值。
//				currentTimeMillis = System.currentTimeMillis();
//				time.set(currentTimeMillis);
//				hourStr = String.valueOf(time.hour);
//				int min=time.minute;
//				if(min>=10){
//					minStr = String.valueOf(min);
//				}else{
//					minStr = "0"+String.valueOf(min);
//				}
//				int sec=time.second;
//				if(sec>=10){
//					secStr = String.valueOf(sec);
//				}else{
//					secStr = "0"+String.valueOf(sec);
//				}
//				
//				
//				if (show_second) { // 显示秒
//					timeStr = hourStr + " : " + minStr + " : " + secStr;
//				} else { // 不显示秒
//					timeStr = hourStr + " : " + minStr;
//				}
//				handler.sendEmptyMessage(0);
//				try {
//					// 为了保证不出现跳秒，改成500毫秒。
//					Thread.sleep(500);
//				} catch (InterruptedException e) {
//					LogUtil.i(TAG, "Thread.sleep...\r\n" + e);
//				}
//			}
//		}
//	};

	// 显示当前时间的线程
	private Runnable myRunnable1 = new Runnable() {
		private long currentTimeMillis;
		private Time time = new Time();
		private String hourStr="";
		private String minStr="";
		private String secStr="";

		@Override
		public void run() {
				// 获取当前时间的时，分，秒值。
				currentTimeMillis = System.currentTimeMillis();
				time.set(currentTimeMillis);
				hourStr = String.valueOf(time.hour);
				int min=time.minute;
				if(min>=10){
					minStr = String.valueOf(min);
				}else{
					minStr = "0"+String.valueOf(min);
				}
				int sec=time.second;
				if(sec>=10){
					secStr = String.valueOf(sec);
				}else{
					secStr = "0"+String.valueOf(sec);
				}
				if (show_second) { // 显示秒
					timeStr = hourStr + " : " + minStr + " : " + secStr;
				} else { // 不显示秒
					timeStr = hourStr + " : " + minStr;
				}
				// 显示当前时间的字符串
				mTime.setText(timeStr);

				if(runFlag){
					handler.postDelayed(this, 500);
				}
				
				
			
		}
	};
	/**
	 * 当控件显示时自动执行的方法
	 */
	@Override
	protected void onAttachedToWindow() {
//		myThread = new Thread(myRunnable);
		runFlag = true;
//		myThread.start();
		
		handler.postDelayed(myRunnable1, 500);
		
		super.onAttachedToWindow();
	}

	
	/**
	 * 当控件从界面退出时自动执行的方法
	 */
	@Override
	protected void onDetachedFromWindow() {
		runFlag = false;
//		myThread.interrupt();
		super.onDetachedFromWindow();
	}

	public MTime getmTime() {
		return mTime;
	}

	public void setmTime(MTime mTime) {
		this.mTime = mTime;
	}

	public boolean isShow_second() {
		return show_second;
	}

	public void setShow_second(boolean show_second) {
		this.show_second = show_second;
	}

	public int getModule_type() {
		return module_type;
	}

	public void setModule_type(int module_type) {
		this.module_type = module_type;
	}

	public int getzOrder() {
		return zOrder;
	}

	public void setzOrder(int zOrder) {
		this.zOrder = zOrder;
	}

	public String getFile_version() {
		return file_version;
	}

	public void setFile_version(String file_version) {
		this.file_version = file_version;
	}

	public String getModule_name() {
		return module_name;
	}

	public void setModule_name(String module_name) {
		this.module_name = module_name;
	}

	public int getModule_uid() {
		return module_uid;
	}

	public void setModule_uid(int module_uid) {
		this.module_uid = module_uid;
	}

	public int getModule_gid() {
		return module_gid;
	}

	public void setModule_gid(int module_gid) {
		this.module_gid = module_gid;
	}
	
	
}